package cn.com.dashihui.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.Store;
import cn.com.dashihui.web.dao.User;
import cn.com.dashihui.web.service.UserMoneyService;
import cn.com.dashihui.web.service.UserService;

@RequiresAuthentication
public class UserMoneyController extends BaseController {
	private UserMoneyService service = new UserMoneyService();
	private UserService userService = new UserService();

	/**
	 * 查询分公司列表
	 */
	public void index() {
		List<Store> storeList = service.findStoreList();
		setAttr("storeList", storeList);
		render("index.jsp");
	}

	/**
	 * 余额明细列表
	 */
	public void page() {
		// 根据登录用户id查询所属公司
		int userid = getCurrentUser().getInt("id");
		String sellerid = service.findSellerId(userid);
		logger.info("userid>>>>"+userid);
		logger.info("sellerid>>>>"+sellerid);
		// 只有 公司id为SE201028362 自营公司OCT和SE233814893
		// 自营公司下的所含人员有权查看钱包明细
		if (userid == 2 || userid == 23 || StrKit.notBlank(sellerid)) {
			if (userid == 2 || userid == 23 || sellerid.equals("SE3002513342") || sellerid.equals("SE233814893")) {
				logger.info("sellerid111>>>>");
				int pageNum = getParaToInt(0, 1);
				String phone = getPara("phone");// 手机号
				String name = getPara("name");// 手机号
				String beginDate = getPara("beginDate");// 时间
				String endDate = getPara("endDate");
				String stores = getPara("stores");// 所属分公司
				String types = getPara("types");// 类别
				int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
				String title = "";
				if (StrKit.notBlank(stores)) {
					List<Store> st = userService.findStoreByIds(stores);
					for (int i = 0; i < st.size(); i++) {
						if (i == 0) {
							title = st.get(i).get("title");
						} else {
							title += "','" + st.get(i).get("title");
						}
					}
				}
				renderResult(0, service.findByPage(pageNum, pageSize, phone, name, beginDate, endDate, types, title));
			} else {
				logger.info("sellerid222>>>>");
				renderResult(1);
				return;
			}
		} else {
			logger.info("sellerid333>>>>");
			renderResult(1);
			return;
		}

	}

	/**
	 * 余额总合
	 */
	public void findMoney() {
		// 根据登录用户id查询所属公司
		int userid = getCurrentUser().getInt("id");
		String sellerid = service.findSellerId(userid);
		// 只有 公司id为SE201028362 自营公司OCT和SE233814893 自营公司的有权查看钱包明细
		if (userid == 20 || userid == 23 || StrKit.notBlank(sellerid)) {
			if (userid == 20 || userid == 23 || sellerid.equals("SE201028362") || sellerid.equals("SE233814893")) {
				String phone = getPara("phone");// 手机号
				String name = getPara("name");// 手机号
				String beginDate = getPara("beginDate");// 时间
				String endDate = getPara("endDate");
				String stores = getPara("stores");// 所属分公司
				String types = getPara("types");// 类别
				String title = "";
				if (StrKit.notBlank(stores)) {
					List<Store> st = userService.findStoreByIds(stores);
					for (int i = 0; i < st.size(); i++) {
						if (i == 0) {
							title = st.get(i).get("title");
						} else {
							title += "','" + st.get(i).get("title");
						}
					}
				}
				double money, money1, nowMoney;

				try {
					Record userMoney;
					Record userMoney1;

					if (StrKit.notBlank(types)) {
						// 查询充值或退款的金额
						if (types.equals("1") || types.equals("3")) {
							userMoney = service.findByMoney(phone, name, beginDate, endDate, types, title);
							nowMoney = userMoney.get("money");
							System.out.println("充值货退款====="+nowMoney);
						} else {
							// 查询消费的金额
							userMoney1 = service.findByMoney1(phone, name, beginDate, endDate, types, title);
							nowMoney = userMoney1.get("money");
							System.out.println("消费===="+nowMoney);
						}
						renderSuccess("nowMoney", nowMoney);
					} else {
						userMoney = service.findByMoney(phone, name, beginDate, endDate, types, title);
						// 查询减少的金额
						userMoney1 = service.findByMoney1(phone, name, beginDate, endDate, types, title);

						System.out.println("userMoney.money的1值是" + userMoney.get("money"));
						System.out.println("userMoney1.money的1值是" + userMoney1.get("money"));

						money = userMoney.get("money");// 增加的
						money1 = userMoney1.get("money");// 减少的的
						nowMoney = money - money1; // nowMoney;总金额
					}

				} catch (Exception e) {
					nowMoney = 0.00;
				}
				System.out.println("nowMoney的值是" + nowMoney);
				renderSuccess("nowMoney", nowMoney);
			} else {
				renderResult(1);
				return;
			}
		} else {
			renderResult(1);
			return;
		}
	}

	/**
	 * 会员详情
	 */
	public void detail() {
		int id = getParaToInt(0, 0);
		if (id != 0) {
			User user = userService.findById(id);
			setAttr("user", user);
			List<Store> storeList = userService.findStoreList();
			setAttr("storeList", storeList);
			List<Record> userList = userService.findUserList(user.get("store"));
			setAttr("userList", userList);
		}
		render("detail.jsp");
	}

	/**
	 * 选择销售
	 */
	public void chooseDao() {
		String[] store = getParaValues("store");
		String str = java.lang.String.join("','", store);
		renderSuccess(userService.chooseDao(str));
	}

	/**
	 * 编辑会员
	 */
	public void doEdit() {
		int userid = getParaToInt("userid", 0);

		String sppoint = getPara("sppoint");
		String jypoint = getPara("jypoint");
		String jkpoint = getPara("jkpoint");

		String phone = getPara("phone");
		String store = getPara("store");
		String toBossOneId = getPara("toBossOneId");
		if (userid != 0) {
			User user = userService.findById(userid);
			if (StrKit.notNull(user)) {
				if (StrKit.notBlank(phone)) {
					if (!user.get("phone").equals(phone)) {
						User users = userService.findByPhone(phone);
						if (StrKit.notNull(users)) {
							renderResult(2);
							return;
						} else {
							user.set("phone", phone);
						}
					}
				}
				if (StrKit.notBlank(jypoint)) {
					user.set("jypoint", jypoint);
				}
				if (StrKit.notBlank(sppoint)) {
					user.set("sppoint", sppoint);
				}
				if (StrKit.notBlank(jkpoint)) {
					user.set("jkpoint", jkpoint);
				}
				if (StrKit.notBlank(store)) {
					Store st = userService.findStoreById(store);
					user.set("store", st.get("title"));
				}
				if (StrKit.notBlank(store) && StrKit.notBlank(toBossOneId)) {
					user.set("toBossOneId", toBossOneId);
				} else if (StrKit.notBlank(store) && StrKit.isBlank(toBossOneId)) {
					user.set("toBossOneId", "");
				}
				if (user.update()) {
					userService.findById(userid);
					renderSuccess();
					return;
				} else {
					renderFailed();
					return;
				}
			} else {
				renderFailed();
				return;
			}
		} else {
			renderFailed();
			return;
		}
	}

	/**
	 * excel导出
	 */
	public void doVipExcel() {
		List<Pair> titles = new ArrayList<Pair>();
		titles.add(new Pair("name", "会员姓名"));
		titles.add(new Pair("phone", "手机号码"));
		titles.add(new Pair("amount", "金额"));
		titles.add(new Pair("createDate", "时间"));
		titles.add(new Pair("type", "类别"));
		titles.add(new Pair("store", "所属分公司"));

		String name = getPara("name");
		String phone = getPara("phone");
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		String types = getPara("types");
		String stores = getPara("stores");
		String title = "";
		if (StrKit.notBlank(stores)) {
			List<Store> st = userService.findStoreByIds(stores);
			for (int i = 0; i < st.size(); i++) {
				if (i == 0) {
					title = st.get(i).get("title");
				} else {
					title += "','" + st.get(i).get("title");
				}
			}
		}
		List<Record> list = service.findExcelVipByPage(phone, name, beginDate, endDate, types, title);

		List<Record> listz = new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record = new Record();
			record.set("name", list.get(i).get("name"));
			record.set("phone", list.get(i).get("phone"));

			// 金额增加与减少
			if (list.get(i).get("type").equals(1)) {
				record.set("amount", "+" + list.get(i).get("amount"));
			} else if (list.get(i).get("type").equals(2)) {
				record.set("amount", "-" + list.get(i).get("amount"));
			}

			record.set("createDate", list.get(i).get("createDate"));
			// 类别
			if (list.get(i).get("orderType").equals(1)) {
				if (list.get(i).get("type").equals(1)) {
					record.set("type", "退款");
				} else if (list.get(i).get("type").equals(2)) {
					record.set("type", "消费");
				}
			} else if (list.get(i).get("orderType").equals(2)) {
				record.set("type", "充值");
			}

			record.set("store", list.get(i).get("store"));
			listz.add(record);
		}
		// 导出
		service.exportVip(getResponse(), getRequest(), titles, listz);
		renderNull();
	}

}